package com.homegoods.orderinfodetail.item;

import com.homegoods.orderinfodetail.R;
import com.homegoods.orderinfodetail.model.SpeclistBean;
import com.kedll.library.adapter.MultiSelectAdapter;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BaseItem;

/**
 * @author QiQi
 * @ClassName: SpeclistItem
 * @date 2018/9/22
 * @description
 */
public class SpeclistItem extends BaseItem<SpeclistBean> {
    private MultiTypeAdapter adapter;

    public SpeclistItem(SpeclistBean data, MultiTypeAdapter adapter) {
        super(data);
        this.adapter = adapter;
        setOnClickListener(v -> {
            getMultiSelectAdapter().toggle(data);
            setSelected(!data.isSelected());
            adapter.notifyItemRangeChanged(0, adapter.getItemCount());
            if (adapter.getOnItemClickListener() != null) {
                adapter.getOnItemClickListener().onItemClicked(v,this);
            }
        });
    }

    private MultiSelectAdapter<SpeclistBean> getMultiSelectAdapter() {
        return (MultiSelectAdapter<SpeclistBean>) adapter;
    }

    public boolean isSelected() {
        return getData().isSelected();
    }

    private void setSelected(boolean selected) {
        for (MultiTypeAdapter.IItem item : getMultiSelectAdapter().getValues()) {
            ((SpeclistItem)item).getData().setSelected(false);
        }
        getData().setSelected(selected);

    }

    public String getName() {
        return getData().getSpename();
    }

    @Override
    public int getLayout() {
        return R.layout.info_item_type;
    }
}
